import {_decorator, Color, Component, Label, Sprite, SpriteFrame, tween, UIOpacity, UITransform, Vec3} from 'cc';
import {Utils} from "db://assets/scripts/utils/Utils";
import {UITipType} from "db://assets/scripts/game/UITipType";
import {PoolManager} from "db://assets/scripts/common/PoolManager";

const {ccclass, property} = _decorator;

/**
 * 提示管理器
 */
@ccclass('TipController')
export class TipController extends Component {
    /**
     * 内容
     */
    @property(Label)
    public tipLabel: Label;
    /**
     * 图标
     */
    @property(Sprite)
    public iconSprite: Sprite;
    /**
     * 背景
     */
    @property(Sprite)
    public bgSprite: Sprite;
    /**
     * 背景不透明度
     */
    @property(UIOpacity)
    public bgOpacity: UIOpacity;
    /**
     * 金币图片
     */
    @property(SpriteFrame)
    public goldSF: SpriteFrame;
    /**
     * 血条图片
     */
    @property(SpriteFrame)
    public heartSF: SpriteFrame;

    /**
     * 移动位置
     */
    private movePosition: Vec3 = new Vec3();
    /**
     * 当前位置
     */
    private currentPosition: Vec3 = new Vec3();
    /**
     * 文字提示的目标位置
     */
    private tipPosition: Vec3 = new Vec3(0, 230, 0);

    /**
     * 播放提示动画
     *
     * @param content 提示内容
     * @param type 提示类型
     * @param position 提示位置
     * @param scale 提示缩放比
     * @param callback 回调
     */
    public show(content: string,
                type: UITipType,
                position: Vec3,
                scale: number,
                callback?: Function) {
        // 设置缩放
        this.node.setScale(scale, scale, scale);

        // 当提示框不存时
        if (!this.tipLabel) {
            // 自我销毁
            PoolManager.getInstance().putNode(this.node);
            return;
        }

        // 获取提示框的大小
        let size = this.tipLabel.node.getComponent(UITransform).contentSize;

        // 设置提示内容
        this.tipLabel.string = content;
        // 设置文字颜色
        this.tipLabel.color = new Color(214, 132, 53, 255);

        // 提示金币或血量时
        if (type === UITipType.GOLD || type === UITipType.HEART) {
            // 关闭背景
            this.bgSprite.enabled = false;
            this.movePosition.set(0, 0, 0);
            this.currentPosition.set(0, 0, 0);
            // 半透明
            this.bgOpacity.opacity = 50;

            // 设置图标
            if (type === UITipType.GOLD) {
                this.iconSprite.spriteFrame = this.goldSF;
            } else if (type === UITipType.HEART) {
                this.iconSprite.spriteFrame = this.heartSF;
            }

            // 设置文字颜色
            this.tipLabel.color = new Color(255, 255, 255, 255);
            // 格式化数字
            this.tipLabel.string = Utils.formatValue(Number(content));

            // 缩放动画
            tween(this.node)
                .to(1.2, {scale: new Vec3(scale, scale, scale)}, {easing: "smooth"})
                .start();

            // 透明度动画
            tween(this.bgOpacity)
                .to(0.8, {opacity: 255}, {easing: "smooth"})
                .to(0.4, {opacity: 0}, {easing: "smooth"})
                .call(() => {
                    callback && callback();
                    PoolManager.getInstance().putNode(this.node);
                })
                .start();
        } else {
            // 纯文字提示
            this.bgSprite.enabled = true;
            this.bgOpacity.opacity = 255;
            this.node.setPosition(position);

            this.iconSprite.node.active = false;

            // 一次性定时器
            this.scheduleOnce(() => {
                // 位置动画
                tween(this.node)
                    .to(1.1, {position: this.tipPosition}, {easing: "smooth"})
                    .call(() => {
                        callback && callback();
                        PoolManager.getInstance().putNode(this.node);
                    })
                    .start();

                // 透明度动画
                tween(this.bgOpacity)
                    .to(0.7, {opacity: 220}, {easing: "smooth"})
                    .to(0.4, {opacity: 0}, {easing: "smooth"})
                    .start();
            }, 0.8);
        }
    }
}